package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        try (
                Connection conn = DBUtil.getConnection();
        ) {
            String str = "SELECT t.name,t.salary,t.title,c.name,c.floor " +
                    "FROM class c " +
                    "JOIN teacher t ON t.id= c.teacher_id " +
                    "WHERE t.name = '王克晶'";
            Statement statement = conn.createStatement();
            ResultSet rs =  statement.executeQuery(str);
            while (rs.next()){
                String tName = rs.getString("t.name");
                int salary = rs.getInt("t.salary");
                String title = rs.getString("t.title");
                String cName = rs.getString("c.name");
                int floor = rs.getInt("c.floor");
                //获取字段时可以使用字段索引, 从1开始,可读性差
//                String tName = rs.getString(1);
//                int salary = rs.getInt(2);
//                String title = rs.getString(3);
//                String cName = rs.getString(4);
//                int floor = rs.getInt(5);
                System.out.println(tName + " " + salary + " " + title + " "+ cName + " " + floor);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
